using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ltf.Remoting.Settings;
using Ltf.Remoting.TeamFoundation;

namespace Ltf.Remoting.Operations
{
    //a comment
	public class Lizardenator
	{
		public void Lizardenate(string folderlist, bool recurse)
		{
			foreach (string folder in folderlist.Split(','))
			{
				AddLizardFolder(folder, recurse);
			}
		}

		public void AddLizardFolder(string folder, bool recurse)
		{
			TFServerConnection connection = ServerConnections.GetConnectionForPath(folder);
			string lizardFolder=connection.GetMetaFolder(folder);
			if (!Directory.Exists(lizardFolder))
			{
				Directory.CreateDirectory(lizardFolder);
				File.WriteAllText(Path.Combine(lizardFolder, "LizardMetaFolder.txt"), folder);
	//			File.SetAttributes(lizardFolder, FileAttributes.Hidden | FileAttributes.System);
				string dtini = "[.ShellClassInfo]\r\nIconFile=%SystemRoot%\\system32\\SHELL32.dll\r\nIconIndex=208";
				File.WriteAllText(Path.Combine(lizardFolder, "Desktop.ini"), dtini);
			}
			if (recurse)
				foreach (string dirName in Directory.GetDirectories(folder, "*.*"))
				{
				//	System.Windows.Forms.MessageBox.Show(dirName);
					if (connection.Settings.IsFolderPathIncluded(dirName))
						AddLizardFolder(dirName, true);
				}
		}

		public void DeLizardenate(string folderlist, bool recurse)
		{
			foreach (string folder in folderlist.Split(','))
			{
				RemoveLizardFolder(folder, recurse);
			}
		}

		void RemoveLizardFolder(string folder, bool recurse)
		{
			string lizardFolder = ServerConnections.GetConnectionForPath(folder).GetMetaFolder(folder);
			if (recurse)
				foreach (string dirName in Directory.GetDirectories(folder))
					RemoveLizardFolder(dirName, true);
			if (Directory.Exists(lizardFolder))
			{
				foreach (string fileName in Directory.GetFiles(lizardFolder))
					File.Delete(fileName);
				Directory.Delete(lizardFolder, true);
			}
		}

	}
}
